﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Job.Web.Framework;
using Job.Logic.Personal;
using Job.Entity;
using Job.Logic.Common;

namespace Job.Web.userControls.Personal
{
    public partial class Certifys : BaseCtrl
    {
        int _resumeId = myR["resumeid"].IntValue;
        int _certifyId = myR["cerid"].IntValue;
        int subjectid = myR["subjectid"].IntValue;

        PersonalLogic pl = new PersonalLogic();

        private void ShowMessage(string message)
        {
            this.message.InnerHtml = "<font color=red>" + message + "</font>";
            this.message.Visible = true;
            this.message.Height = "30px";
            this.message.VAlign = "middle";
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            this.btnSave.Attributes.Add("onclick", "return checkInput();");

            if (!Page.IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {

            try
            {
                //初始化月份
                for (int i = 1; i <= 12; i++)
                {
                    ddlMonth.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }

                //初始化年份
                for (int i = 1949; i <= DateTime.Now.Year; i++)
                {
                    ddlYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }


                string code = "";
                string nameCode = "";
                JobPersonCertify certify = pl.GetCertify(_certifyId);
                if (certify != null)
                {
                    txtOther.Text = certify.Other;
                    txtResult.Text = certify.Result;
                    if (certify.CertifyName != null)
                    {
                        code = Constants.GetParentCodeBySubCode(certify.CertifyName.Trim());
                        nameCode = certify.CertifyName.Trim();
                    }
                    lblMode.Text = "编辑证书";
                }
                else
                {
                    lblMode.Text = "新增证书";
                }
                WebCommon.LoadLevelDropDownList(ddlName1, ddlName2, RelatedListName, code, nameCode, Constants.DictionaryType.CERTIFICATE_NAME);

                gvCertify.DataSource = pl.GetCertifyList(_resumeId);
                gvCertify.DataBind();
            }
            catch (Exception ex)
            {

                ShowMessage("查询失败：<br>" + ex.Message);
                Logger.WriteLog("证书查询失败：" + ex.Message);
            }
        }

        protected void gvCertify_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void gvCertify_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int id = Convert.ToInt32(e.CommandArgument);
            if (e.CommandName.ToLower() == "edit")
            {
                //edit
                string url = WebCommon.GetApplicationPath(Request) + "Person/PersonalResume.aspx?categoryid=1&subjectid=" + this.subjectid + "&resumeid=" + this._resumeId + "&cerid=" + id;
                Response.Redirect(url);

            }
            else if (e.CommandName.ToLower() == "delete")
            {
                JobPersonCertify cer = pl.GetCertify(id);
                pl.DeleteCertify(cer);

                string url = WebCommon.GetApplicationPath(Request) + "Person/PersonalResume.aspx?categoryid=1&subjectid=" + this.subjectid + "&resumeid=" + this._resumeId;
                Response.Redirect(url);
            }

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                JobPersonCertify cer = new JobPersonCertify();
                cer.GetYear = Convert.ToInt32(ddlYear.SelectedValue);
                cer.GetMonth = Convert.ToInt32(ddlMonth.SelectedValue);
                cer.Other = txtOther.Text;
                cer.Result = txtResult.Text;
                cer.PersonInfoId = this._resumeId;
                cer.CertifyName = ddlName2.SelectedValue == null ? "" : ddlName2.SelectedValue.Trim();
                cer.PersonCertifyId = this._certifyId;
                cer.PersonInfoId = _resumeId;

                if (this._certifyId > 0)
                {
                    pl.UpdateCertify(cer);

                }
                else
                {
                    pl.InsertCertify(cer);
                }

            }
            catch (Exception ex)
            {

                ShowMessage("保存失败：<br>" + ex.Message);
                Logger.WriteLog("证书保存失败：" + ex.Message);
                return;
            }
            string url = WebCommon.GetApplicationPath(Request) + "Person/PersonalResume.aspx?categoryid=1&subjectid=" + this.subjectid + "&resumeid=" + this._resumeId;
            Response.Redirect(url);

        }

        protected void gvCertify_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblCerName = (Label)e.Row.FindControl("lblCerName");
                Label lblName = (Label)e.Row.FindControl("lblName");
                Label lblOther = (Label)e.Row.FindControl("lblOther");

                lblCerName.Text = MemoryDictionary.GetFullCertificateName(lblName.Text.Trim());
                if (lblCerName.Text.Trim().Length < 1)
                {
                    lblCerName.Text = lblOther.Text;
                }
            }
        }

    }
}